package com.mimc.controller.admin;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import com.mimc.core.model.SysAppVersion;
import com.mimc.core.service.SysAppVersionService;
import com.mimc.framework.domain.AjaxResult;
import com.mimc.framework.domain.KV;
import io.jboot.db.model.Columns;
import io.jboot.utils.StrUtil;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

@Slf4j
@RequestMapping("/admin/sys/appversion")
public class SysAppVersionController extends JbootController {
    @Inject
    private SysAppVersionService sysAppVersionService;
    public AjaxResult lists(){
        Integer page=getInt("page",1);
        Integer pageSize=getInt("pageSize",15);
        String versionName=getPara("versionName");
        String platform=getPara("versionName");
        Columns columns=Columns.create();
        if(StrUtil.isNotEmpty(versionName)){
            columns.like("version_name","%"+versionName+"%");
        }
        if(StrUtil.isNotEmpty(versionName)){
            columns.like("platform","%"+platform+"%");
        }
        Page<SysAppVersion> list=sysAppVersionService.paginateByColumns(page,pageSize, columns,"create_time desc");
        return AjaxResult.success("",list);
    }

    public AjaxResult add(){
        String versionName=getPara("versionName");
        String versionCode=getPara("versionCode");
        Boolean force=getBoolean("force",false);
        Boolean silence=getBoolean("silence",false);
        Integer packageType=getInt("packageType");
        Boolean issue=getBoolean("issue",false);
        String downloadUrl=getPara("downloadUrl");
        String platform=getPara("platform");
        String names=getPara("names");
        List<KV> kvlist= JSON.parseArray(names,KV.class);
        Record version = new Record();
        version.set("version_name",versionName);
        version.set("version_code",versionCode);
        version.set("package_type",packageType);
        version.set("force",force);
        version.set("silence",silence);
        version.set("issue",issue);
        version.set("download_url",downloadUrl);
        version.set("platform",platform);
        version.set("create_time",new Date());
        version.set("update_time",new Date());
        for (int i=0;i<kvlist.size();i++){
            KV kv=kvlist.get(i);
            version.set(kv.getKey(),kv.getValue());
        }
        Db.save("sys_app_version", version);
        return AjaxResult.success();
    }
    public AjaxResult update(){
        Integer versionId=getInt("versionId");
        String versionName=getPara("versionName");
        String versionCode=getPara("versionCode");
        Boolean force=getBoolean("force",false);
        Boolean silence=getBoolean("silence",false);
        Integer packageType=getInt("packageType");
        Boolean issue=getBoolean("issue",false);
        String downloadUrl=getPara("downloadUrl");
        String platform=getPara("platform");
        String names=getPara("names");
        List<KV> kvlist= JSON.parseArray(names,KV.class);
        Record version = Db.findById("sys_app_version","version_id",versionId);
        version.set("version_name",versionName);
        version.set("version_code",versionCode);
        version.set("package_type",packageType);
        version.set("force",force);
        version.set("silence",silence);
        version.set("issue",issue);
        version.set("download_url",downloadUrl);
        version.set("platform",platform);
        version.set("update_time",new Date());
        for (int i=0;i<kvlist.size();i++){
            KV kv=kvlist.get(i);
            version.set(kv.getKey(),kv.getValue());
        }
        Db.update("sys_app_version","version_id", version);
        return AjaxResult.success();
    }
    public AjaxResult delete(){
        Integer versionId=getInt("versionId");
        Db.deleteById("sys_app_version","version_id", versionId);
        return AjaxResult.success();
    }
}
